package com.classops.demo

import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.classops.demo.repo.UserRepository
import com.classops.demo.service.ApiService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.koin.java.KoinJavaComponent.get
import org.koin.java.KoinJavaComponent.inject

class MainViewModel(application: Application) : AndroidViewModel(application) {

    private val userRepo: UserRepository = get(UserRepository::class.java)

    private val apiService: ApiService by inject(ApiService::class.java)

    fun test() {
        viewModelScope.launch {
            val name = userRepo.getUserName()
            Log.e("Test", "name: $name")

            try {
                val res = withContext(Dispatchers.IO) {
                    apiService.getTestData()
                }
                println("${res}")
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

}